package com.twang.stock.task;

import com.twang.stock.entity.User;
import com.twang.stock.mapper.UserMapper;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.task.old.BaseTask;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.List;

/**
 * 复盘提醒
 */
@Slf4j
@Component
public class FuPanTask extends BaseTask {

    @Resource
    private JavaMailSenderImpl mailSender;
    @Autowired
    private  CommonService  commonService;
    @Resource
    private UserMapper userMapper;

     //@Scheduled(cron = "0 0 8,12,15,22 * * ? ")
    public void excute() {
        //
        List<String> ids = Arrays.asList(commonService.getValue("warn_ID").toString().split(","));
        for (String id : ids) {
            User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
            if (null != user) {
                adminFuPan(user.getEmail());
            }
        }

    }

    public void adminFuPan(String emailAddress){
        log.info("FuPanTask ---> start");
        try {
            StringBuffer content = new StringBuffer();
            content.append("<div><div style='margin-left:4%;'>");
            content.append("<p style='color:red;'>");
            content.append("复盘啦：").append(HolidayCopy.getRealToday()).append("</p>");
            content.append("<p style='text-indent: 2em;'>").append("</p>");
            content.append("<p style='text-indent: 2em;display: block;word-break: break-all;'>");
            content.append("<a style='text-decoration: none;'></a></p>");
            String 飙升榜 = "http://www.stock520.cn/ths/reDianHotRank";
            content.append("飙升榜：：<a style='text-decoration: none;' href='" + 飙升榜 + "'>" + 飙升榜 + "</a></p>");
            String 涨停板 = "http://www.stock520.cn/zhangData";
            content.append("涨停板：<a style='text-decoration: none;' href='" + 涨停板 + "'>" + 涨停板 + "</a></p>");
            String 成交额 = "http://www.stock520.cn/chengJiaoE";
            content.append("成交额：<a style='text-decoration: none;' href='" + 成交额 + "'>" + 成交额 + "</a></p>");
            String 龙虎榜 = "http://www.stock520.cn/lhb";
            content.append("龙虎榜(晚7点跟新)：<a style='text-decoration: none;' href='" + 龙虎榜 + "'>" + 龙虎榜 + "</a></p>");
            String 主线轴 = "http://www.stock520.cn/zhuxian";
            content.append("主线轴：<a style='text-decoration: none;' href='" + 主线轴 + "'>" + 主线轴 + "</a></p>");
            String 年度涨幅前20 = "http://www.stock520.cn/ndzfb";
            content.append("年度涨幅前20：<a style='text-decoration: none;' href='" + 年度涨幅前20 + "'>" + 年度涨幅前20 + "</a></p>");
            //
            String 同花顺热度24小时 = "http://www.stock520.cn/ths/select24HourData";
            content.append("同花顺热度24小时：<a style='text-decoration: none;' href='" + 同花顺热度24小时 + "'>" + 同花顺热度24小时 + "</a></p>");
            content.append("</div>");
            content.append("<ul style='color: rgb(169, 169, 189);font-size: 18px;'>");
            content.append("<li>为了保障您帐号的安全，该链接有效期为12小时。</li>");
            content.append("<li>该链接只能使用一次，请周知。</li>");
            content.append("<li>如果该链接无法点击，请直接复制以上网址到浏览器地址栏中访问。</li>");
            content.append("<li>请您妥善保管，此邮件无需回复。</li>");
            content.append("</ul>");
            content.append("</div>");
            MimeMessage mimeMailMessage = mailSender.createMimeMessage();
            //简单邮件
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage, true, "utf-8");
            messageHelper.setFrom("精研 V3.0<wangtao_13140520@163.com>");
            messageHelper.setTo(emailAddress);
            messageHelper.setSubject("复盘提醒：" + HolidayCopy.getToday年月日分时秒(HolidayCopy.getRealToday(), HolidayCopy.getHHmmss().substring(8,12)));
            messageHelper.setText(content.toString());
            mailSender.send(mimeMailMessage);
        }catch(Exception e) {

        }
    }
}
